package com.wnxy.admin.controller;

import cn.hutool.core.net.SSLUtil;
import com.wnxy.admin.common.ResponseResult;
import com.wnxy.admin.common.handler.GlobalVariable;
import com.wnxy.admin.common.util.AliOSSUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

@RestController
@RequestMapping("/upload")
public class UploadController {
    private final AliOSSUtils aliOSSUtils;
    private final RedisTemplate redisTemplate;

    public UploadController(AliOSSUtils aliOSSUtils, RedisTemplate redisTemplate) {
        this.aliOSSUtils = aliOSSUtils;
        this.redisTemplate = redisTemplate;
    }

    @PostMapping("/file")
    public String upload(@RequestParam("file") MultipartFile file) throws IOException {
        String url = aliOSSUtils.upload(file);
        String fileName = AliOSSUtils.extractFileName(url);
        //将图片的名称存储到redis集合中--set
        redisTemplate.opsForSet().add("upload-file-all", fileName);
        return fileName;
    }
}
